home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / games1 / drain2.zip / DRAIN.C next >
Text File  |  1993-01-04  |  5KB  |  228 lines

  1. /* Drain, version 2: better prompt handling.
  2.  
  3.     I don't claim credit for this idea, and the original perpretrator
  4.     is lost in the mists of time...but I liked the idea. The original
  5.     program only had one problem, though: It ignored the user's
  6.     PROMPT setting. That made it somewhat transparent, since not many
  7.     people any more run from drive A, and darned few of them use a
  8.     yellow prompt! I got Turbo C, and decided this would be a good
  9.     learning project.
  10.  
  11.     This program is (C) Copyright 1987, James R. Maynard III
  12.                         6027 Leafwood Circle
  13.                         League City, Texas 77573
  14.     This program may be copied and distributed freely, with only
  15.     one restriction: These comments must not be modified. (After
  16.     all, we all would like to be known for SOMETHING!) If you come
  17.     up with improvements, please let me know so I can enjoy them
  18.     too. Send me mail at the above address, or to my GEnie mailbox
  19.     (JAYMAYNARD), my Compu$erve mailbox (71036,1603), or my FidoNode
  20.     (Graf Spee, 106/64). I hope to have a usenet address soon...
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <dir.h>
  25. #include <math.h>
  26. #include <bios.h>
  27. #include <dos.h>
  28.  
  29. #define MAXPROMPT 80
  30.  
  31. main()
  32. {
  33.     char promptstr[MAXPROMPT];
  34.     void getprompt(), clrscr(), draindrive();
  35.  
  36.     getprompt(promptstr);    /* get the user's prompt, or default */
  37.     clrscr();        /* clear the screen? */
  38.     printf("%s",promptstr);    /* write the bogus prompt */
  39.     getch();        /* wait for a key */
  40.     draindrive();        /* do the draining */
  41. }
  42.  
  43. void getprompt(str)
  44. char *str;
  45. {
  46.     char *envprompt, *getenv(), promptchar;
  47.     int putpath(), putdate(), puttime(), putver();
  48.  
  49.     if ((envprompt = getenv("PROMPT")) != NULL) {
  50.         for ( ; (promptchar = *envprompt) != 0; envprompt++, str++) {
  51.             if (promptchar == '$') {
  52.                 promptchar = *++envprompt;
  53.                 switch (promptchar) {
  54.                     case '$': *str = '$';
  55.                           break;
  56.                     case '_': *str = '\n';
  57.                           break;
  58.                     case 'T':
  59.                     case 't': str += puttime(str);
  60.                           break;
  61.                     case 'D':
  62.                     case 'd': str += putdate(str);
  63.                           break;
  64.                     case 'N':
  65.                     case 'n': *str = getdisk()+'A';
  66.                           break;
  67.                     case 'G':
  68.                     case 'g': *str = '>';
  69.                           break;
  70.                     case 'L':
  71.                     case 'l': *str = '<';
  72.                           break;
  73.                     case 'B':
  74.                     case 'b': *str = '|';
  75.                           break;
  76.                     case 'H':
  77.                     case 'h': *str = '\b';
  78.                           break;
  79.                     case 'E':
  80.                     case 'e': *str = '\x1B';
  81.                           break;
  82.                     case 'Q':
  83.                     case 'q': *str = '=';
  84.                           break;
  85.                     case 'P':
  86.                     case 'p': str += putpath(str);
  87.                           break;
  88.                     case 'V':
  89.                     case 'v': str += putver(str);
  90.                           break;
  91.                 }
  92.             }
  93.             else *str = promptchar;
  94.         }
  95.         *str = '\0';
  96.     }
  97.     else {
  98.         *str++ = getdisk() + 'A';
  99.         strcpy(str,">");
  100.     }
  101. }
  102.  
  103. int putpath(char *str)
  104. {
  105.     char *curdir;
  106.     int pathlen;
  107.  
  108.     curdir = getcwd(NULL,80);
  109.     strcpy(str,curdir);
  110.     pathlen = strlen(curdir) - 1;
  111.     free(curdir);
  112.     return(pathlen);
  113. }
  114.  
  115. int putver(char *str)
  116. {
  117.     int i;
  118.     FILE *verfile;
  119.     char version[MAXPROMPT];
  120.  
  121.     system("ver >$$drain$.$$$");
  122.     verfile = fopen("$$drain$.$$$","r");
  123.     fgets(version,MAXPROMPT,verfile);
  124.     fgets(version,MAXPROMPT,verfile);
  125.     strcpy(str,version);
  126.     fclose(verfile);
  127.     unlink("$$drain$.$$$");
  128.     return(strlen(version) - 2);
  129. }
  130.  
  131. int puttime(char *str)
  132. {
  133.     int i;
  134.     struct time now;
  135.  
  136.     gettime(&now);
  137.     sprintf(str,"%2d:%02d:%02d.%02d",now.ti_hour,now.ti_min,now.ti_sec,
  138.         now.ti_hund);
  139.     return(10);
  140. }
  141.  
  142. int putdate(char *str)
  143. {
  144.     int i;
  145.     long secs_now;
  146.     struct date today;
  147.  
  148.     getdate(&today);
  149.     time(&secs_now);
  150.     strncpy(str,ctime(&secs_now),4);
  151.     str += 4;
  152.     sprintf(str,"%2d-%02d-%04d",today.da_mon,today.da_day,today.da_year);
  153.     return(13);
  154. }
  155.  
  156. void clrscr()
  157. {
  158.     union REGS regs;
  159.  
  160.     regs.h.ah = 15; /* get video mode */
  161.     int86(0x10,®s,®s);
  162.     regs.h.ah = 0;  /* set video mode, left in AL by previous call */
  163.     int86(0x10,®s,®s);
  164. }
  165.  
  166. void draindrive()
  167. {
  168.     void tone(int hertz, int ticks);
  169.     void slidetone(int start, int finish, int inc);
  170.     int freq, i;
  171.     char curdrive;
  172.  
  173.     curdrive = getdisk() + 'A';
  174.     puts(" *** SYSTEM ERROR ***");
  175.     printf("Water has been detected in drive %c. Drive cleanup",curdrive);
  176.     puts(" in progress.\nThis will take a few moments.");
  177.     puts("\nDrain phase beginning...");
  178.     for (i = 8000; i > 50; i *= 0.95) {
  179.         freq = rand() / 32768.0 * i + 50.0;
  180.         tone(freq,1);
  181.     }
  182.     puts("Drain phase complete.\n");
  183.     sleep(2);
  184.     puts("Spin dry phase beginning...");
  185.     for (i = 200; i <= 1000; i += 200) {
  186.         slidetone(i - 200,i,1);
  187.         tone(i,40);
  188.     }
  189.     slidetone(1000,0,-5);
  190.     puts("Spin dry phase complete.\n\nDrive cleanup complete.");
  191.     puts("You may continue your work.");
  192. }
  193.  
  194. void tone(int hertz, int ticks)
  195. {
  196.     long begintime;
  197.     char bits;
  198.     int count;
  199.  
  200.     begintime = biostime(0,0L);
  201.     bits = inportb(0x61);
  202.     count = 1193280 / hertz;
  203.     outportb(0x43,0xB6);
  204.     outportb(0x42,count & 0xFF);
  205.     outportb(0x42,count >> 8);
  206.     outportb(0x61,bits | 0x03);
  207.     while (biostime(0,0L) < begintime + ticks)
  208.         ;    /* do nothing */
  209.     outportb(0x61,bits);
  210. }
  211.  
  212. void slidetone(int start, int finish, int inc)
  213. {
  214.     char bits;
  215.     int count, hertz, j;
  216.  
  217.     bits = inportb(0x61);
  218.     outportb(0x61,bits | 0x03);
  219.     for (hertz = start; hertz != finish; hertz += inc) {
  220.         count = 1193280 / hertz;
  221.     /*    outportb(0x43,0xB6); */
  222.         outportb(0x42,count & 0xFF);
  223.         outportb(0x42,count >> 8);
  224.         for (j = 0; j < 300; j++) ;
  225.     }
  226.     outportb(0x61,bits);
  227. }
  228.